import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, Params } from "@angular/router";
import { Observable } from "rxjs/Observable";

import { TableTab } from "../f10-shared/interface-shared";
import { scroll } from "../f10-shared/common-methods";
import { removeHMS, compare } from "../f10-shared/util";
import { TableHeader } from "../f10-shared/interface-shared";
import { F10Service } from "../service/f10.service";
import { ScrollService } from "../service/scroll.service";

@Component({
  templateUrl: 'capital.component.html',
  styleUrls: ['capital.component.less']
})

export class CapitalComponent implements OnInit {
  timer = null;
  tabClicked = {
    flag: false
  };
  activeTabIndex = 0;
  scrollTimer = null;
  mustChange = false;
  subLists: TableTab[] = [
    {
      label: "募集资金情况",
      class: 'sub-title-0'
    },
    {
      label: "项目进度",
      class: 'sub-title-1'
    },
    {
      label: "收购兼并",
      class: 'sub-title-2'
    }
  ]
  placementsHeader: TableHeader[] = [
    {
      label: '公告日期',
      key: 'initialInfoPublDate'
    },
    {
      label: '发行类别',
      key: 'category'
    },
    {
      format: true,
      label: '实际募集资金净额(元)',
      key: 'netProceeds'
    },
    {
      label: '发行起始日',
      key: 'startDate'
    }
  ]
  placementsSource: Observable<any>;
  investsHeader: TableHeader[] = [
    {
      label: '项目名称',
      key: 'investProject'
    },
    {
      label: '信息发布日期',
      key: 'infoPublDate'
    },
    {
      label: '截至日期',
      key: 'actualInvestEndDate'
    },
    {
      format: true,
      label: '计划投资(元)',
      key: 'plannedSum'
    },
    {
      format: true,
      label: '已投入募集资金(元)',
      key: 'actualSum'
    },
    {
      label: '收购兼并类型',
      key: 'purchaseType'
    },
    {
      label: '进展和收益说明',
      key: 'proceedingStatement',
      span: 'expaned'
    }
  ]
  investsSource: Observable<any>;
  // 收购兼并
  mnasHeader: TableHeader[] = [
    {
      label: "信息发布日期",
      key: "infoPublDate"
    },
    {
      label: "收购兼并类型",
      key: "purchaseType"
    },
    {
      format: true,
      label: "收购资产价格(元)",
      key: "purchasePrice"
    },
    {
      label: "收购权益比例(%)",
      key: "equityRatio"
    },
    {
      label: "收购标的名称",
      key: "targetName"
    },
    {
      label: "项目出让方",
      key: "transferor"
    }
  ]
  mnasSource: Observable<any>;
  purchaseTypeDict: string[] = ['兼并', '收购无形资产', '收购实物资产', '收购股权', '其他收购']
  constructor (
    private f10Service: F10Service,
    private router: ActivatedRoute,
    private scrollService: ScrollService
  ) {}

  ngOnInit () {
    let symbolObservable = this.router.parent.parent.params.map(params => params["id"]).filter(id => !!id).map(id => {
      return {
        symbol: (<string>id).replace("_", ".")
      };
    })
    // 募集资金
    this.placementsSource = symbolObservable
      .switchMap((stock) => this.f10Service.getPlacements(stock.symbol))
      .map(data => data.map((d, i) => ({
        ...d,
        initialInfoPublDate: removeHMS(d.initialInfoPublDate),
        startDate: removeHMS(d.startDate),
        netProceeds: d.netProceeds
      })).sort(compare('initialInfoPublDate', 'des')))
    // 项目进度
    this.investsSource = symbolObservable
      .switchMap((stock) => this.f10Service.getInvests(stock.symbol))
      .map(data => data.map((d, i) => ({
        ...d,
        plannedSum: d.plannedSum,
        actualSum: d.actualSum,
        infoPublDate: removeHMS(d.infoPublDate),
        actualInvestEndDate	: removeHMS(d.actualInvestEndDate	),
        expaned: false
      })).sort(compare('infoPublDate', 'des')))
    // 收购兼并
    this.mnasSource = symbolObservable
      .switchMap((stock) => this.f10Service.getMnas(stock.symbol))
      .map(data => data.map((d, i) => ({
        ...d,
        purchasePrice: d.purchasePrice,
        purchaseType: d.purchaseType - 1 >= 0 ? this.purchaseTypeDict[d.purchaseType - 1] : '--',
        infoPublDate: removeHMS(d.infoPublDate)
      })).sort(compare('infoPublDate', 'des')))
  }

  tabToggle (event) {
    this.tabClicked.flag = true
    this.timer = scroll(`segment-container`, event, this.timer, this.tabClicked);
  }

  scrollEvent (e, el, el1, el2, el3, el4, el5) {
    if (this.scrollTimer) clearTimeout(this.scrollTimer)
    this.scrollTimer = setTimeout(() => {
      this.mustChange = !this.mustChange
      this.tabClicked.flag = false
      this.activeTabIndex = this.scrollService.tabIndex(e, el, el1, el2, el3, el4, el5)
    }, 50)
    if (this.tabClicked.flag) return
    this.activeTabIndex = this.scrollService.tabIndex(e, el, el1, el2, el3, el4, el5)
  }
}